<?php
namespace addons\hellozjx_mapwxapp\inc\page;

trait Map 
{
	public $table_area = 'hellozjx_mapwxapp_area';
	public $table_shop = 'hellozjx_mapwxapp_shop';

	public function doPageGetMapList()
	{
		global $_W, $_GPC;
        $uniacid = $_W['uniacid'];
        $area_id = intval($_GPC['area_id']);
		$lat=$_GPC['lat'];
		$lng=$_GPC['lng'];
		$config = $this->module['config'];
        $where = array('uniacid' => $uniacid, 'enabled' => 1);
        if ($area_id > 0) {
            $where['area_id'] = array(0, $area_id);
        }
        $shop = pdo_getall($this->table_shop, $where, array('id','name','logo','shop_lat','shop_lng','shop_lat_t','shop_lng_t'), '', array('displayorder desc', 'id desc'));
		$arr=array();
        if (!empty($shop)) {
            foreach ($shop as $key => $value) {
				$d= $this->getDistance($value['shop_lat_t'], $value['shop_lng_t'], $lat, $lng);
				if($d>$config['distance']&&$config['distance']>0)
				{
					continue;
				}
				else
				{
					$arr[$key]=$value;
					$arr[$key]['dist']=$d;
					$arr[$key]['name'].=",距您".$d."千米";
					$arr[$key]['logo'] = tomedia($value['logo']);
				}
            }
        }
		$arr = array_values($arr);
        $total = pdo_getcolumn($this->table_shop, $where, 'count(*)');
        $data = array('list' => $arr ?: array(), 'total' => $total ? intval($total) : 0);
        message(error(0, $data), '', 'ajax');
	}

	public function doPageMapTeacher()
	{
		global $_W, $_GPC;
        $uniacid = $_W['uniacid'];
        $area_id = intval($_GPC['area_id']);
		$lat=$_GPC['lat'];
		$lng=$_GPC['lng'];
		$config = $this->module['config'];
        $where = array('status' => 1);
        if (empty($area_id)) {
        	$shop = pdo_getall($this->table_teacher, $where, array('id','name','avatar','latitude','longitude'), '', array('sort asc'));
        } else {
        	$sql = "select b.id,b.name,b.avatar,b.latitude,b.longitude from " . tablename($this->table_room_teacher) . " as a left join " . tablename($this->table_teacher) . " as b on a.type_id = $area_id where a.type = 1 and b.status = 1 and a.tid = b.id";
        	$shop = pdo_fetchall($sql);
        }
        
        $arr=array();
        if (!empty($shop)) {
            foreach ($shop as $key => $value) {
				$d= $this->getDistance($value['latitude'], $value['longitude'], $lat, $lng);
				if($d>$config['distance']&&$config['distance']>0)
				{
					continue;
				}
				else
				{
					$arr[$key]=$value;
					$arr[$key]['dist']=$d;
					$arr[$key]['name'].=",距您".$d."千米";
					$arr[$key]['avatar'] = tomedia($value['avatar']);
				}
				$arr[$key]['shop_lat'] = $value['latitude'];
				$arr[$key]['shop_lng'] = $value['longitude'];
				$arr[$key]['shop_lat_t'] = $value['latitude'];
				$arr[$key]['shop_lng_t'] = $value['longitude'];
				// 拼装出该老师下面的第一个视频
				$video = pdo_get($this->table_video, array('teacher_id' => $value['id']), array('id'));
				$arr[$key]['id'] = $video['id'];
            }
        }
		$arr = array_values($arr);
        $total = pdo_getcolumn($this->table_teacher, $where, 'count(*)');
        $data = array('list' => $arr ?: array(), 'total' => $total ? intval($total) : 0);
        message(error(0, $data), '', 'ajax');
	}
}